package cn.kgc.springbootdemo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.kgc.springbootdemo.annotation.PassToken;
import cn.kgc.springbootdemo.entity.User;
import cn.kgc.springbootdemo.form.RegisterUserForm;
import cn.kgc.springbootdemo.form.UpdateUserForm;
import cn.kgc.springbootdemo.form.UpdateUserInfoForm;
import cn.kgc.springbootdemo.form.UserPageForm;
import cn.kgc.springbootdemo.service.UserService;
import cn.kgc.springbootdemo.util.R;
import cn.kgc.springbootdemo.vo.UserLoginVO;
import cn.kgc.springbootdemo.vo.UserVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;


@Api(value = "UserController", tags = "用户管理控制器")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Validated
@Slf4j
public class UserController {
    private final UserService userService;

    /**
     * 校验用户账号是否存在
     *
     * @param userCode 用户账号
     * @return 结果
     */
    @ApiOperation(value = "校验用户账号是否存在", notes = "校验注册用户账号是否存在")
    @GetMapping("/checkUserCode")
    @PassToken
    public R<Boolean> checkUserCode(
            @ApiParam(name = "userCode", value = "用户账号", required = true)
            @RequestParam
            @NotBlank(message = "用户Code不能为空")
                    String userCode) {
        return userService.checkUserCode(userCode);
    }


    /**
     * 注册
     *
     * @param registerUserForm 注册
     * @return 结果
     */
    @PassToken
    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public R<Boolean> register(HttpServletRequest request, @RequestBody @Valid RegisterUserForm registerUserForm) {
        Boolean register = userService.register(request, registerUserForm);
        if (register) {
            return R.ok(true, "恭喜你注册成功");
        }
        return R.failed("注册失败");
    }


    /**
     * 修改密码
     *
     * @param updateUserForm 修改密码表单对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改密码")
    @PostMapping("/updatePassword")
    public R<Boolean> updateUserPassword(@RequestBody @Valid UpdateUserForm updateUserForm) {
        return R.ok(userService.updateUserPassword(updateUserForm));
    }


    @ApiOperation(value = "删除用户")
    @GetMapping("/deleteUser/{id}")
    public R<Boolean> deleteUser(@ApiParam(value = "用户ID", required = true) @PathVariable String id) {
        return R.ok(userService.deleteUser(id));
    }


    /**
     * 分页用户查询
     *
     * @param userPageForm 分页查询对象
     * @return 查询结果集合
     */
    @ApiOperation(value = "分页以及用户姓名查询")
    @PostMapping("/selectUsersByLimit")
    public R<IPage<UserVO>> selectUsersByLimit(@RequestBody @Valid UserPageForm userPageForm) {
        return R.ok(userService.selectUsersByLimit(userPageForm));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据用户ID查询用户登录信息")
    public R<UserLoginVO> getUserLoginInfoById(@ApiParam("用户Id") @PathVariable String id) {
        return R.ok(userService.getUserLoginInfoById(id));
    }

    @PostMapping("/updateUserInfo")
    @ApiOperation("更新个人用户信息")
    public R<Boolean> updateUserInfo(@RequestBody @Valid UpdateUserInfoForm updateUserInfoForm) {
        User user = BeanUtil.copyProperties(updateUserInfoForm, User.class);
        if (user.getIdCard().contains("*")) {
            user.setIdCard(null);
        }
        if (user.getPhone().contains("*")) {
            user.setPhone(null);
        }
        return R.ok(user.updateById());
    }

}
